package com.szbber.medical.door

import android.view.View
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.szbber.commen.base.BaseRoutePath
import com.szbber.commen.base.BaseVMActivity
import com.szbber.commen.bean.DoorPatientBean
import com.szbber.commen.view.AreaClickListener
import com.szbber.medical.door.databinding.DoorActivityHomeBinding
import com.szbber.medical.door.databinding.DoorItemBannerBinding
import com.szbber.medical.door.viewModel.HomeActivityVM
import kotlinx.android.synthetic.main.door_activity_home.*


@Route(path = BaseRoutePath.door_home)
class HomeActivity : BaseVMActivity<DoorActivityHomeBinding, HomeActivityVM>() {

    override fun bindLayout(): Int = R.layout.door_activity_home

    override fun initVM(): HomeActivityVM = ViewModelProvider(this,
        ViewModelProvider.NewInstanceFactory()).get(HomeActivityVM::class.java)

    override fun initView() {
        mBinding.homeVM = mViewModel
        tvTitle.setOnClickListener(object :
            AreaClickListener(5, 500, object : AreaClickListener.ClickEvent {
                override fun onEvent() {
                   open(BaseRoutePath.app_setting)
                }
            }) {
        })
        mViewModel.getDoorPatient(this)
        mViewModel.getDoorName()
        mViewModel.doorPatient.observe(this) {
            mBinding.banner.setAutoPlayAble(it.data.list.size>1)
            mBinding.banner.setData(R.layout.door_banner, it.data.list, null)
            mBinding.banner.setAdapter { banner, itemView, model, position ->
                val mRecyclerView = itemView.findViewById<RecyclerView>(R.id.mRecyclerView)
                val adapter = object :
                    BaseQuickAdapter<DoorPatientBean.DataX, BaseViewHolder>(R.layout.door_item_banner,
                        it.data.list.get(position).toMutableList()) {
                    override fun convert(holder: BaseViewHolder, item: DoorPatientBean.DataX) {
                        val binding =
                            DataBindingUtil.bind<ViewDataBinding>(holder.itemView) as DoorItemBannerBinding
                        binding.bannerVM = item
                    }
                }
                mRecyclerView.adapter = adapter
                val manager = GridLayoutManager(this, 3)
                mRecyclerView.layoutManager = manager
            }
        }
    }

}